<?php

namespace Biz\model\wiki;

use Doraemon\tools\ImgUrl;

class BrandAreaModule extends \Doraemon\pockets\datebase\BaseDbModel
{
    const _CONFIG_FILE_ = 'db_store';

    const _CONFIG_SELECT_ = 'db_store';

    const _DATABASE_ = 'store';

    const _TABLE_NAME = 'brand_area_module';

    const STATUS_NORMAL = 1;
    const STATUS_DELETE = 9;

    const HOT_BRAND_AREA = 2;//品牌计划B品牌专区热门品牌数据源

    public static function admDel($id)
    {
        $where = [
            'id' => $id,
        ];
        $data['edittime'] = date("Y-m-d H:i:s");
        $data['module_status'] = self::STATUS_DELETE;
        return self::update($data, $where)->rowCount();
    }

    public static function admSort($sort)
    {
        foreach ($sort as $id => $sort) {
            $where['id'] = $id;
            $data = [
                'sort' => $sort,
                'edittime' => date("Y-m-d H:i:s"),
            ];
            self::update($data, $where)->rowCount();
        }
        return ;
    }

    public static function admGetInfo($id)
    {
        $where = [
            'id' => $id,
            'module_status' => self::STATUS_NORMAL,
        ];
        return self::get('*', $where);
    }

    public static function admSaveInfo($id, $data)
    {
        if (self::admGetInfo($id)) {
            $where = [
                'id' => $id,
                'module_status' => self::STATUS_NORMAL,
            ];
            $data['edittime'] = date("Y-m-d H:i:s");
            return self::update($data, $where)->rowCount();
        } else {
            $data['addtime'] = date("Y-m-d H:i:s");
            self::insert($data);
            return self::id();
        }
    }

    private static function formatImgInfo($imgInfo)
    {
        $imgInfo['img_url'] = ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $imgInfo['pic_id'], ImgUrl::IMAGE_SIZE_OP_ORI);
        return $imgInfo;
    }

    public static function getImgByModuleId($brand_area_id)
    {
        $imgList= self::select("*",
            [
                'AND' => [
                    'brand_area_id' => $brand_area_id,
                    'module_status' => self::STATUS_NORMAL
                ],
                'ORDER' => [
                    'sort',
                    'id'
                ]
            ]);

        if ($imgList) {
            foreach ($imgList as &$img) {
                $img['module_id'] = 'module_' . $img['id'];
                $img = self::formatImgInfo($img);
            }
        }
        return $imgList;
    }

    public static function getAllByWhere($where = [],$files = '*')
    {
        if(!empty($where)){
            $where = array_merge($where, ['module_status'=>self::STATUS_NORMAL]);
        }else{
            $where =  ['module_status'=>self::STATUS_NORMAL];
        }
        $data = self::select($files, $where);
        $data = array_map(function ($_data){
            return self::_filter($_data);
        },$data);
        return $data;
    }

    public static function _filter($data){
        foreach($data as $_k=>&$_v){
            switch ($_k){
                case 'pic_id':
                    $data['pic_url'] = !empty($_v) ? ImgUrl::getUrl(ImgUrl::APPTYPE_OP, $_v, ImgUrl::IMAGE_SIZE_OP_ORI) : '';
                    break;
            }
        }
        return $data;
    }

    public static function getOneByWhere( $where = [], $files = '*')
    {
        $data = self::get($files, $where);
        $result[0] = $data;
        $result = array_map(function ($_data){
            return self::_filter($_data);
        },$result);
        return $result[0];
    }

    public static function addData($data)
    {
        try {
            self::insert($data);
            return self::id();
        } catch (Exception $e) {
            return false;
        }
    }

    public static function updateData($data, $where)
    {
        try {
            self::update($data, $where);
            return true;
        } catch (Exception $e) {
            return false;
        }
    }

}